package net.sync.disruptor;

import com.lmax.disruptor.dsl.Disruptor;

import java.util.concurrent.CountDownLatch;

//创建一个发送消费事件 的类，用于发送要处理的消息

/**
 * 生产者
 */
public class TradeTransactionPublisher implements Runnable {
    Disruptor<TradeTransactionEvent> disruptor;
    private CountDownLatch latch;
    //生产者发送10条消息
    private static int LOOP = 10;

    public TradeTransactionPublisher(CountDownLatch latch, Disruptor<TradeTransactionEvent> disruptor) {
        this.disruptor = disruptor;
        this.latch = latch;
    }

    @Override
    public void run() {
        TradeTransactionEventTranslator tradeTransloator = new TradeTransactionEventTranslator();
        for (int i = 0; i < LOOP; i++) {
            System.out.println("生产者成功向Ringbuffer中发送了一条消息");
            disruptor.publishEvent(tradeTransloator);
        }
        latch.countDown();
    }
}